﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Copy5xConsole
{
    class Command
    {
        public string CommandId { get; set; }
        public List<Param> Args { get; set; }

        public static List<Command> ParseCommand(string[] args)
        {
            int i = 1;
            List<Command> commends = new List<Command>();

            try
            {
                if (args.Length > 0)
                {
                    if (args[0].StartsWith("?") || args[0].ToLower() == "man" || args[0].ToLower() == "help")
                    {
                        commends.Add(createManCommand());
                    }
                    else
                    {
                        do
                        {
                            i--;

                            string commandId = args[i++].Replace("-", string.Empty).ToLower();

                            List<Param> param = new List<Param>();

                            int index = 0;

                            if (args.Length > i)
                            {
                                string arg = args[i++];

                                while (!arg.StartsWith("-") && i <= args.Length)
                                {
                                    if (arg.IndexOf('=') < 0)
                                        arg = "=" + arg;

                                    string[] _param = arg.Split('=');

                                    param.Add(new Param { Index = index, Name = _param[0], Value = _param[1] });

                                    index++;

                                    if (args.Length > i)
                                        arg = args[i++];
                                    else
                                        i++;
                                }
                            }
                            else
                            {
                                i++;
                            }

                            commends.Add(new Command { CommandId = commandId, Args = param });

                        } while (i <= args.Length);

                    }
                }
                else
                    commends.Add(createManCommand());

            }
            catch (Exception exp)
            {
                Console.WriteLine(exp);

                commends.Add(createManCommand());
            }

            return commends;
        }

        /// <summary>
        /// Create manual Commands
        /// </summary>
        /// <returns></returns>
        private static Command createManCommand()
        {
            return new Command { CommandId = "?", Args = null };
        }

        public override string ToString()
        {
            string outs = "Id: " + CommandId;
            outs += Environment.NewLine + "Params { " + Environment.NewLine;
            if (Args != null)
            {
                foreach (Param param in Args)
                {
                    outs += "\t" + param.ToString() + Environment.NewLine;
                }
            }
            outs += "}";
            return outs;
        }
    }

    class Param
    {
        public string Name { get; set; }
        public string Value { get; set; }
        public int Index { get; set; }

        public override string ToString()
        {
            return "Index:[" + Index + "] Name:[" + Name + "] Value:[" + Value + "]";
        }
    }
}
